package com.hui.platform.falseworkapi.service.business.user.controller;

import com.hui.platform.falseworkapi.common.core.vo.PageData;
import com.hui.platform.falseworkapi.common.valid.groups.*;
import com.hui.platform.falseworkapi.common.web.annotation.ApiResult;

import com.hui.platform.falseworkapi.service.business.user.service.TbAuthoritiesService;
import com.hui.platform.falseworkapi.service.business.user.bo.TbAuthoritiesBO;
import com.hui.platform.falseworkapi.service.business.user.dto.TbAuthoritiesDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import java.util.List;

/**
 * 权限表 前端控制器
 *
 * @author hui
 * @since 2020-04-17
 */
@Api(tags = "权限表-相关接口")
@ApiResult
@Validated
@RestController
@RequestMapping("/user/tb-authorities")
public class TbAuthoritiesController {

    private final TbAuthoritiesService tbAuthoritiesService;

    @Autowired
    public TbAuthoritiesController(TbAuthoritiesService tbAuthoritiesService) {
        this.tbAuthoritiesService = tbAuthoritiesService;
    }

    //-------接口-------

    @GetMapping("/{id}")
    @ApiOperation("获取一条权限表记录")
    public TbAuthoritiesBO readById(@ApiParam("权限表Id") @PathVariable @Min(1) Long id) {
        return tbAuthoritiesService.readById(id);
    }

    @GetMapping("/ids")
    @ApiOperation("获取权限表记录列表--Id数组")
    public List<TbAuthoritiesBO> readByIds(@ApiParam("3D自建房Id数组") @RequestParam @NotEmpty List<Long> ids) {
        return tbAuthoritiesService.readByIds(ids);
    }

    @GetMapping("/list")
    @ApiOperation("获取权限表记录列表--条件")
    public List<TbAuthoritiesBO> readList(@Validated(Read.class) TbAuthoritiesDTO condition) {
        return tbAuthoritiesService.readList(condition);
    }

    @PostMapping("/page")
    @ApiOperation("获取权限表记录列表--分页条件")
    public PageData<TbAuthoritiesBO> readPage(@ApiParam("页码") @RequestParam @NotNull @Min(1) Long pageNum,
                                        @ApiParam("每页大小") @RequestParam @NotNull @Min(1) @Max(100) Long pageSize,
                                        @ApiParam("查询条件") @RequestBody @Validated(Paging.class) TbAuthoritiesDTO condition) {
        return tbAuthoritiesService.readPage(pageNum, pageSize, condition);
    }

    @PostMapping("/")
    @ApiOperation("新建一条权限表记录")
    @PreAuthorize("@auth.hasAuthority('user:tb-authorities:create')")
    public Long createOne(@ApiParam("权限表DTO信息") @RequestBody @Validated({Default.class, Create.class}) TbAuthoritiesDTO dto) {
        return tbAuthoritiesService.createOne(dto);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新一条权限表记录")
    @PreAuthorize("@auth.hasAuthority('user:tb-authorities:update')")
    public Long updateOne(@ApiParam("权限表Id") @PathVariable @Min(1) Long id,
                          @ApiParam("修改信息") @RequestBody @Validated({Default.class, Update.class}) TbAuthoritiesDTO dto) {
        return tbAuthoritiesService.updateOne(id, dto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除一条权限表记录")
    @PreAuthorize("@auth.hasAuthority('user:tb-authorities:delete')")
    public Long deleteById(@ApiParam("权限表Id") @PathVariable @Min(1) Long id) {
        return tbAuthoritiesService.deleteById(id);
    }

    @DeleteMapping("/")
    @ApiOperation("删除批量权限表记录--Id数组")
    @PreAuthorize("@auth.hasAuthority('user:tb-authorities:delete')")
    public List<Long> deleteByIds(@ApiParam("权限表Id数组") @RequestParam @NotEmpty List<Long> ids) {
        return tbAuthoritiesService.deleteByIds(ids);
    }

}

